package org.advanced.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * @author John.zhang
 * @version Id: Buffered, v 1.0 2020/12/14 14:07 ZhiYong Exp $
 * 缓存流处理的效率会比磁盘操作效率高(会高出五倍多，当然如果设置buffer每次读取的字节变多也会提升非缓冲流的效率)
 * 缓冲区默认8kb，内部的一个优化
 */
public class Buffered {

    public static void main(String[] args) {
        try (
                BufferedInputStream bufferedInputStream =
                        new BufferedInputStream(new FileInputStream("C:/data/io/io.txt"));
                //缓冲区默认8kb
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("C:/data/io/io01.txt"))) {
            int len = 0;
            byte[] buffer = new byte[1024];
            while ((len = bufferedInputStream.read(buffer)) != -1) {
                System.out.println(new String(buffer, 0, len));
                bufferedOutputStream.write(buffer, 0, len);
                //TODO
                bufferedOutputStream.flush();
            }
        } catch (Exception e) {

        }

    }

}
